package com.workflow.service.impl;

import java.util.List;

import com.base.bean.Cardinfo;
import com.common.bean.Datadict;
import com.common.util.AbstractManager;
import com.common.util.StringToken;
import com.workflow.bean.Outputcard;
import com.workflow.service.OutputcardService;

public class OutputcardServiceImpl extends AbstractManager implements
		OutputcardService {

	public void addOutputcard(String cardno, String outdetailno) {
		//应修改对应卡信息的状态 流转状态
		Outputcard outputcard = new Outputcard();
		outputcard.setCardno(cardno);
		outputcard.setOutdetailno(outdetailno);
		
		this.getHibernateTemplate().saveOrUpdate(outputcard);
		
		Cardinfo cardinfo = null;
		cardinfo = (Cardinfo) this.getHibernateTemplate().load(Cardinfo.class, cardno);
		Datadict datadictByStatus = new Datadict();
		//置卡状态为锁定状态  75l
		datadictByStatus.setTypeid(StringToken.DICT_CARDSTATUS_LOCK);
		cardinfo.setDatadictByStatus(datadictByStatus);
		cardinfo.setBelongto(outdetailno);
		this.getHibernateTemplate().update(cardinfo);
	}

	@SuppressWarnings("unchecked")
	public List<Outputcard> findAllOutputcard() {
		String hql = "from Outputcard order by outdetailno,cardno";
		return this.getHibernateTemplate().find(hql);
	}

	@SuppressWarnings("unchecked")
	public List<Outputcard> findAllOutputcardByOutdetailno(String outdetailno) {
		String hql = "from Outputcard card where card.outdetailno = ? order by card.cardno";
		return this.getHibernateTemplate().find(hql,outdetailno);
	}

	public Outputcard findOutputcard(String cardno) {
		return (Outputcard) this.getHibernateTemplate().load(Outputcard.class, cardno);
	}

	public void removeOutputcard(String cardno) {
		Outputcard outputcard = null;
		outputcard = (Outputcard) this.getHibernateTemplate().load(Outputcard.class, cardno);
		if(outputcard != null) {
			this.getHibernateTemplate().delete(outputcard);
			
			Cardinfo cardinfo = null;
			cardinfo = (Cardinfo) this.getHibernateTemplate().load(Cardinfo.class, cardno);
			Datadict datadictByStatus = new Datadict();
//			置卡状态为库存 状态编号 76l
			datadictByStatus.setTypeid(StringToken.DICT_CARDSTATUS_STORE);
			cardinfo.setDatadictByStatus(datadictByStatus);
			cardinfo.setBelongto("");//取消所属状态，临时
			this.getHibernateTemplate().update(cardinfo);
		}
	}

	public void updateOutputcard(String cardno, String outdetailno) {
		Outputcard outputcard = new Outputcard();
		outputcard.setCardno(cardno);
		outputcard.setOutdetailno(outdetailno);
		
		this.getHibernateTemplate().saveOrUpdate(outputcard);
		
		Cardinfo cardinfo = null;
		cardinfo = (Cardinfo) this.getHibernateTemplate().load(Cardinfo.class, cardno);
		Datadict datadictByStatus = new Datadict();
		//置卡状态为锁定状态  75l
		datadictByStatus.setTypeid(StringToken.DICT_CARDSTATUS_LOCK);
		cardinfo.setDatadictByStatus(datadictByStatus);
		cardinfo.setBelongto(outdetailno);
		this.getHibernateTemplate().update(cardinfo);
	}

}
